package com.cictec.middleware.gps.engine.position.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.commons.utils.log.MultiLog;
import com.cictec.middleware.gps.engine.BusInstance;
import com.cictec.middleware.gps.engine.exception.BusPositionHandlerException;
import com.cictec.middleware.gps.engine.position.BusPositionHandler;
import com.cictec.middleware.gps.engine.utils.MultiLogUtil;
import com.cictec.middleware.gps.service.BusLineService;

/**
 * 相对位置
 * @author ql
 * @date 2017年9月26日
 */
@Component
public class BusPositionRelativeDistanceHandler implements BusPositionHandler {

	private Logger logger = LoggerFactory.getLogger(BusPositionRelativeDistanceHandler.class);
	
	@Autowired
	private BusLineService busLineService;

	@Override
	public void onHandler(BusInstance busInstance) throws BusPositionHandlerException {

		MultiLog log = MultiLogUtil.getPositionJudgeLog(busInstance);

		log.debug("\t【相对位置】");
		if (!busInstance.isInDeviate() 
//				&& busInstance.isInOperateTime() 
				&& busInstance.isLineTypeExist()
				&& !busInstance.isInStandBy()) {

			// 计算 进出站及 相对位置
			try {
				caculateRealtiveDistanceLine(busInstance);
				caculateRealtiveDistanceStation(busInstance);
			} catch (Exception e) {
				e.printStackTrace();
				log.debug("BusPositionRelativeDistanceHandler  获取站点信息异常 {}", e);
			}

			log.debug("\t【相对位置】【线路】相对距离：{}", busInstance.getPositionRate());
			log.debug("\t【相对位置】【站点】相对距离：{}", busInstance.getPositionStationRate());
			logger.debug("\t【相对位置】【线路】相对距离：{}", busInstance.getPositionRate());
			logger.debug("\t【相对位置】【站点】相对距离：{}", busInstance.getPositionStationRate());

		} else {

			busInstance.setPositionRate("");
			busInstance.setPositionStationRate("");

			log.debug("\t【不做】【相对位置】原因： 运营状态:{}, 偏线状态：{}, 线路上下行:{}", busInstance.isInOperateTime(),
					busInstance.isInDeviate(), busInstance.getLineType());
			logger.debug("\t【不做】【相对位置】原因： 运营状态:{}, 偏线状态：{}, 线路上下行:{}", busInstance.isInOperateTime(),
					busInstance.isInDeviate(), busInstance.getLineType());
		}

	}

	/**
	 * 【相对距离计算】【线路】计算相对于线路的距离
	 */
	public void caculateRealtiveDistanceLine(BusInstance busInstance) {

		Integer orgStationSeq = null;
		if (!PMSUtils.isEmpty(busInstance.getStationSeq())) {
			if (busInstance.isInStation()) {
				orgStationSeq = Integer.parseInt(busInstance.getStationSeq()); // 原来出站站序
			} else {
				orgStationSeq = Integer.parseInt(busInstance.getStationSeq()) - 1; // 原来出站站序
			}
			if (orgStationSeq < 1) {
				orgStationSeq = 1;
			}
		}
		double rate = busLineService.caculateRealtiveDistanceByLine(busInstance.getLineUuid(),
				busInstance.getLineType(), busInstance.getLatitude(), busInstance.getLongitude(), orgStationSeq);

		busInstance.setPositionRate(String.valueOf(rate));
	}

	/**
	 * 【相对距离计算】【站点间】计算相对应站点间的距离
	 */
	public void caculateRealtiveDistanceStation(BusInstance busInstance) {
		// 计算车辆的相对位置
		double rate = busLineService.caculateRealtiveDistanceByStation(busInstance.getLineUuid(),
				busInstance.getLineType(), busInstance.getLatitude(), busInstance.getLongitude(),
				busInstance.getStationSeq(), busInstance.getStationEnterOut());
		busInstance.setPositionStationRate(String.valueOf(rate));
	}

}
